package com.example.zhangwei.widgts;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.graphics.Point;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

public class DrawMoveLine extends View
{
	private int mWidth; 
	private int mHeight; 
	
	private final int mViewPadd = 60; 	
	private int mTableRowHeight ; 
	private int mPointWidth ; 
	
	private ArrayList<Point> mPointList;
	

	public DrawMoveLine(Context context, AttributeSet attrs) 
	{
		super(context, attrs);
		// TODO Auto-generated constructor stub
		mPointList = new ArrayList<Point>();
	}
	
	@Override
	protected void onFinishInflate() {
		// TODO Auto-generated method stub
		super.onFinishInflate();
		
	}
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		// TODO Auto-generated method stub
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		mWidth = getWidth();
		mHeight = getHeight();
		mTableRowHeight = (mHeight - mViewPadd*2)/10;
		mPointWidth = (mWidth - mViewPadd*2)/20;
	}
	
	@Override
	protected void onDraw(Canvas canvas) 
	{
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		
		drawTable(canvas);
		drawPointLine(canvas);
		moveingLine();
	}
	
	private void drawTable(Canvas canvas)
	{
		Paint paint = new Paint();
		paint.setColor(Color.WHITE);
		
		//画外框
		paint.setStyle(Paint.Style.STROKE);
		Rect rec = new Rect(0 + mViewPadd, 0 + mViewPadd, mWidth - mViewPadd , mHeight - mViewPadd);
		canvas.drawRect(rec, paint);
		
		String str = "信号强度 [dBm]";
		int strWidth = (int)paint.measureText(str) + 20;
		//左边文字
		Path textPath = new Path();
		paint.setStyle(Paint.Style.FILL);
		paint.setTextSize(14);
		paint.setAntiAlias(true);
		textPath.moveTo(20, mHeight/2 + strWidth/2);
		textPath.lineTo(20, mHeight/2 - strWidth/2);
		canvas.drawTextOnPath(str, textPath, 0, 0, paint);
		
		//左侧数字
		for (int i = 0 ; i < 11 ; i++)
		{
			canvas.drawText( i + "v", mViewPadd - 25 , mViewPadd + mTableRowHeight * i + 2, paint);
		}
		
		//画表格虚线
		Path path = new Path();     
        PathEffect effects = new DashPathEffect(new float[]{2,2,2,2},1);
        paint.setStyle(Paint.Style.STROKE);
        paint.setAntiAlias(false);
        paint.setPathEffect(effects); 
        for (int i = 0 ; i < 10 ; i++)
		{
        	path.moveTo( mViewPadd , mViewPadd + mTableRowHeight * i);
        	path.lineTo( mWidth-mViewPadd, mViewPadd + mTableRowHeight * i);
        	canvas.drawPath(path, paint);
		}
	}
	
	private void drawPointLine(Canvas canvas)
	{
		Paint paint = new Paint();
		paint.setStyle(Paint.Style.FILL);
		paint.setColor(Color.WHITE);
		paint.setAntiAlias(true);
		paint.setStrokeWidth(2);
		
		if(mPointList.size() >= 2)
		{
			for(int i = 0; i<mPointList.size()-1; i++){
				canvas.drawLine(mPointList.get(i).x, mPointList.get(i).y, 
						mPointList.get(i+1).x, mPointList.get(i+1).y, paint);
			}
		}
	}
	
	private void moveingLine()
	{
		int pointY = mViewPadd + (int)(Math.random()*(mHeight - mViewPadd*2));
		Point p = new Point(mWidth - mViewPadd , pointY);
		if(mPointList.size() > 21){
			mPointList.remove(0);
			for(int i = 0; i<20; i++){
				if(i == 0) mPointList.get(i).x -= (mPointWidth - 2);
				else mPointList.get(i).x -= mPointWidth;
			}
			mPointList.add(p);
		}
		else{
			for(int i = 0; i<mPointList.size()-1; i++){
				mPointList.get(i).x -= mPointWidth;
			}
			mPointList.add(p);
		}
	}

}
